home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / pgp23src.zip / ZUNZIP.C < prev    next >
C/C++ Source or Header  |  1993-05-09  |  4KB  |  116 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unzip.c
  4.  
  5.   Highly butchered minimum unzip code for inflate.c
  6.  
  7.   ---------------------------------------------------------------------------*/
  8.  
  9. #include "zunzip.h"              /* includes, defines, and macros */
  10.  
  11. #define VERSION  "v4.20p BETA of 2-18-92"
  12.  
  13. /**********************/
  14. /*  Global Variables  */
  15. /**********************/
  16.  
  17. longint csize;        /* used by list_files(), ReadByte(): must be signed */
  18. static longint ucsize;       /* used by list_files(), unReduce(), unImplode() */
  19.  
  20. ULONG mask_bits[] =
  21. {0x00000000L,
  22.  0x00000001L, 0x00000003L, 0x00000007L, 0x0000000fL,
  23.  0x0000001fL, 0x0000003fL, 0x0000007fL, 0x000000ffL,
  24.  0x000001ffL, 0x000003ffL, 0x000007ffL, 0x00000fffL,
  25.  0x00001fffL, 0x00003fffL, 0x00007fffL, 0x0000ffffL,
  26.  0x0001ffffL, 0x0003ffffL, 0x0007ffffL, 0x000fffffL,
  27.  0x001fffffL, 0x003fffffL, 0x007fffffL, 0x00ffffffL,
  28.  0x01ffffffL, 0x03ffffffL, 0x07ffffffL, 0x0fffffffL,
  29.  0x1fffffffL, 0x3fffffffL, 0x7fffffffL, 0xffffffffL};
  30.  
  31. /*---------------------------------------------------------------------------
  32.     Input file variables:
  33.   ---------------------------------------------------------------------------*/
  34.  
  35. byte *inbuf = NULL, *inptr;     /* input buffer (any size is legal) and pointer */
  36. int incnt;
  37.  
  38. ULONG bitbuf;
  39. int bits_left;
  40. boolean zipeof;
  41.  
  42. int zipfd;               /* zipfile file handle */
  43.  
  44. /*---------------------------------------------------------------------------
  45.     Output stream variables:
  46.   ---------------------------------------------------------------------------*/
  47.  
  48. byte *outbuf;                   /* buffer for rle look-back */
  49. byte *outptr;
  50. byte *outout;                   /* scratch pad for ASCII-native trans */
  51. longint outpos;                 /* absolute position in outfile */
  52. int outcnt;                     /* current position in outbuf */
  53. int outfd;
  54.  
  55. /*---------------------------------------------------------------------------
  56.     unzip.c static global variables (visible only within this file):
  57.   ---------------------------------------------------------------------------*/
  58.  
  59. static byte *hold;
  60.  
  61. /*******************/
  62. /* Main unzip code */
  63. /*******************/
  64.  
  65. int unzip( FILE *inFile, FILE *outFile )        /* return PK-type error code (except under VMS) */
  66. {
  67.     int status = 0;
  68.     outfd = fileno( outFile );
  69.     zipfd = fileno( inFile );
  70.  
  71.     inbuf = (byte *) (malloc(INBUFSIZ + 4));    /* 4 extra for hold[] (below) */
  72.     outbuf = (byte *) (malloc(OUTBUFSIZ + 1));  /* 1 extra for string termin. */
  73.     outout = outbuf;        /*  else just point to outbuf */
  74.  
  75.     if ((inbuf == NULL) || (outbuf == NULL) || (outout == NULL)) {
  76.         fprintf(stderr, "error:  can't allocate unzip buffers\n");
  77.         RETURN(4);              /* 4-8:  insufficient memory */
  78.     }
  79.     hold = &inbuf[INBUFSIZ];    /* to check for boundary-spanning signatures */
  80.  
  81.     bits_left = 0;
  82.     bitbuf = 0;
  83.     outpos = 0L;
  84.     outcnt = 0;
  85.     outptr = outbuf;
  86.     zipeof = 0;
  87.  
  88.     /* Set output buffer to initial value */
  89.     memset(outbuf, 0, OUTBUFSIZ);
  90.  
  91.     /* Go from high- to low-level I/O */
  92.     lseek( zipfd, ftell(inFile), SEEK_SET );
  93.  
  94.     if ((incnt = read(zipfd, (char *) inbuf,INBUFSIZ)) <= 0) {
  95.         fprintf(stderr, "error: unexpected end if input");
  96.         status = -1;               /*  can still do next file   */
  97.     }
  98.     inptr = inbuf;
  99.  
  100.     /* Read in implode information */
  101.     csize = 1000L;            /* Dummy size just to get input bits */
  102.  
  103.     /* Get compressed, uncompressed file sizes */
  104.     csize = ucsize = 1000000000L;    /* Make sure we can read in anything */
  105.     if (status == 0)
  106.         inflate();        /* Ftoomschk! */
  107.  
  108.     /* Flush output buffer before returning */
  109.     if (status == 0 && FlushOutput())
  110.         status = -1;
  111.     free(inbuf);
  112.     free(outbuf);
  113.     inbuf = outbuf = outout = NULL;
  114.     return(status);
  115. }
  116.